// pages/my/my.js
const app = getApp();
const {
  loginOut,
  myInfo,
  exitTeam
} = require('../../config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    app.appRequest({
      url: myInfo,
      success: (res) => {
        let data = res.data;
        this.setData({
          data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.showNavigationBarLoading();
    app.appRequest({
      url: myInfo,
      success: (res) => {
        wx.hideNavigationBarLoading();
        let data = res.data;
        this.setData({
          data
        })
        if (res.data.haveNews > 0) {
          wx.showTabBarRedDot({
            index: 1,
          })
        } else {
          wx.hideTabBarRedDot({
            index: 1,
          })
        }
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading()
    app.appRequest({
      url: myInfo,
      success: (res) => {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        let data = res.data;
        this.setData({
          data
        })
        if (res.data.haveNews > 0) {
          wx.showTabBarRedDot({
            index: 1,
          })
        }else{
          wx.hideTabBarRedDot({
            index: 1,
          })
        }
      }
    })
  },
  changeUser: function() {
    wx.showModal({
      title: '切换账户',
      content: '请您确认是否要切换账户',
      success(res) {
        if (res.confirm) {
          app.appRequest({
            url: loginOut,
            method: 'POST',
            success: (res) => {
              if (res.code == 0) {
                app.globalData.token = null
                wx.reLaunch({
                  url: '/pages/login_mobile/login_mobile?type=0',
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  navigatorToUserInfo: function(e) {
    let {
      avatar,
      name
    } = e.currentTarget.dataset;
    // console.log(11, avatar)
    wx.navigateTo({
      url: `/pages/userInfo/userInfo?avatar=${avatar}&name=${name}`,
    })
  },
  navigatorToTeamNews: function() {
    wx.navigateTo({
      url: '/pages/teamNews/teamNews',
    })
  },
  navigatorToMyTeam: function() {
    wx.navigateTo({
      url: '/pages/myTeam/myTeam',
    })
  },
  navigatorToHelpCenter: function(){
    wx.navigateTo({
      url: '/pages/helpCenter/helpCenter',
    })
  },
  navigatorToFeedback: function(){
    wx.navigateTo({
      url: '/pages/feedback/feedback',
    })
  },
  navigatorToNotice_list: function(){
    wx.navigateTo({
      url: '/pages/notice_list/notice_list',
    })
  }
})